<?php

define('MAX_PER_PAGE', 30);

function LogTry($c, $s, $to, $time)
{
	global $mcrp;
	global $mcuser;
	$html = '<tr class="try ' . ($s == true?'success':'failed') . ' ' . ($to == true?'timeout':'') . '">';
	$html .= '<td>' . date('d/m/Y - H:i:s') . '</td>';
	$html .= '<td><a href="http://www.wolframalpha.com/input/?i=' . $_SERVER['REMOTE_ADDR'] . '">' . $_SERVER['REMOTE_ADDR'] . '</a></td>';
	$html .= '<td><a href="./../profil.php?id=' . $mcuser->id . '">' . $mcuser->name . ' (' . $mcuser->id . ')</a></td>';
	$html .= '<td><a href="./../contest.php?id=' . $c->id . '">' . stripslashes($c->title) . ' (' . $c->id . ')</a></td>';
	$html .= '<td>' . $time . '</td>';
	ob_start();
	var_dump($_POST);
	$post = ob_get_contents();
	ob_end_clean();
	ob_start();
	var_dump($mcuser);
	$user = ob_get_contents();
	ob_end_clean();
	ob_start();
	var_dump($_SESSION['var']);
	$var = ob_get_contents();
	ob_end_clean();
	ob_start();
	var_dump($_SESSION);
	$session = ob_get_contents();
	ob_end_clean();
	$html .= '<td><div><a href="#" onclick="ToggleDisplay(this);return false;">User</a><br /><pre style="display:none;">' . $user . '</pre></div></td>';
	$html .= '<td><div><a href="#" onclick="ToggleDisplay(this);return false;">Post</a><br /><pre style="display:none;">' . $post . '</pre></div></td>';
	$html .= '<td><div><a href="#" onclick="ToggleDisplay(this);return false;">Var</a><br /><pre style="display:none;">' . $var . '</pre></div></td>';
	$html .= '<td><div><a href="#" onclick="ToggleDisplay(this);return false;">Session</a><br /><pre style="display:none;">' . $session . '</pre></div></td>';
	$html .= '</tr>';
	$data = file_get_contents($mcrp . 'log/try.dat');
	$data = explode(' ', $data);
	file_put_contents($mcrp . 'log/try.log.' . $data[0], $html, FILE_APPEND);
	$data[1]++;
	if($data[1] >= MAX_PER_PAGE) { $data[0]++; $data[1] = 0; }
	file_put_contents($mcrp . 'log/try.dat', implode(' ', $data));
}

function LogLogin($u, $auto)
{
	global $mcrp;
	$text = '[' . date('d/m/Y - H:i:s') . '][' . $_SERVER['REMOTE_ADDR'] . '] : ' . $u->name . '(' . $u->id . ')' . ($auto == true?'[autologin]':'') . "\r\n";
	file_put_contents($mcrp . 'log/login.log', $text, FILE_APPEND);
}

function LogBruteForce($u, $c)
{
	global $mcrp;
	global $mcuser;
	$html = '<tr class="bruteforce hiddenbf">';
	$html .= '<td>' . date('d/m/Y - H:i:s') . '</td>';
	$html .= '<td><a href="http://www.wolframalpha.com/input/?i=' . $_SERVER['REMOTE_ADDR'] . '">' . $_SERVER['REMOTE_ADDR'] . '</a></td>';
	$html .= '<td><a href="./../profil.php?id=' . $u->id . '">' . $u->name . ' (' . $u->id . ')</a></td>';
	$html .= '<td>' . ($c === false?'[invalide]':'<a href="./../contest.php?id=' . $c->id . '">' . stripslashes($c->title) . ' (' . $c->id . ')</a>') . '</td>';
	$html .= '<td></td>';
	ob_start();
	var_dump($_POST);
	$post = ob_get_contents();
	ob_end_clean();
	ob_start();
	var_dump($u);
	$user = ob_get_contents();
	ob_end_clean();
	ob_start();
	var_dump($_SESSION);
	$session = ob_get_contents();
	ob_end_clean();
	$html .= '<td><div><a href="#" onclick="ToggleDisplay(this);return false;">User</a><br /><pre style="display:none;">' . $user . '</pre></div></td>';
	$html .= '<td><div><a href="#" onclick="ToggleDisplay(this);return false;">Post</a><br /><pre style="display:none;">' . $post . '</pre></div></td>';
	$html .= '<td></td>';
	$html .= '<td><div><a href="#" onclick="ToggleDisplay(this);return false;">Session</a><br /><pre style="display:none;">' . $session . '</pre></div></td>';
	$html .= '</tr>';
	$data = file_get_contents($mcrp . 'log/try.dat');
	$data = explode(' ', $data);
	file_put_contents($mcrp . 'log/try.log.' . $data[0], $html, FILE_APPEND);
	$data[1]++;
	if($data[1] >= MAX_PER_PAGE) { $data[0]++; $data[1] = 0; }
	file_put_contents($mcrp . 'log/try.dat', implode(' ', $data));
}

function LogCompilation($code)
{
	global $mcrp;
	global $mcuser;
	$text = '[' . date('d/m/Y - H:i:s') . '][' . $_SERVER['REMOTE_ADDR'] . '] : ' . $mcuser->name . '(' . $mcuser->id . ")\n" . $code . "\n";
	file_put_contents($mcrp . 'log/compilation.log', $text, FILE_APPEND);
}

function LogUser()
{
	global $mcuser;
	$text = '[' . date('d/m/Y - H:i:s') . '][' . $_SERVER['REMOTE_ADDR'] . '] ' . ($_SERVER['REQUEST_URI']) . "\n";
	file_put_contents($mcrp . 'log/user/' . $mcuser->id . '.log', $text, FILE_APPEND);
}

?>